//
// Created by gastonfeng on 2023/11/11.
//

#ifndef RTE_CSP280AV3_IP175_H
#define RTE_CSP280AV3_IP175_H

#include <Arduino.h>
#include "smodule.h"
#include "pb_common.h"

class IP175 : public smodule {

    int mdc;
    int mdio;
public:
    IP175(int mck, int mdio) {
        this->mdc = mck;
        this->mdio = mdio;
    }

    void GPIO_SET_MDIO_MODE_INPUT();

    void GPIO_SET_MDIO_MDC_MODE_INPUT();

    void _MIIM_DRIVER_CLOCK_PULSE();

    void _MIIM_DRIVER_START();

    void _MIIM_DRIVER_OP_CODE_READ();

    void _MIIM_DRIVER_OP_CODE_WRITE();

    void _MIIM_DRIVER_PHY_REG_OUTPUT(uint8_t PHY, uint8_t REG);

    void _MIIM_DRIVER_TA_READ();

    void _MIIM_DRIVER_TA_WRITE();

    void _MIIM_DRIVER_WRITE_DATA(uint16_t data);

    uint16_t _MIIM_DRIVER_READ_DATA();

    void MIIM_DRIVER_WRITE(uint8_t PHY, uint8_t REG, uint16_t DATA);

    uint16_t MIIM_DRIVER_READ(uint8_t PHY, uint8_t REG);

    int begin(uint32_t tick) override;

    int run(uint32_t tick) override;

    int type() override { return pb_object_ip175_tag; }

    int diag(uint32_t tick);

#ifndef THIS_IS_BOOTLOADER

    const char *name() override { return "IP175"; }
#endif
#ifdef __PLATFORMIO_BUILD_DEBUG__
    int dev_test(uint32_t tick) override;

#endif

    int encode(pb_object *msg, pb_ostream_t *stream) override {
        msg->object.ip175 = data;
        msg->which_object = pb_object_ip175_tag;
        return pb_encode(stream, pb_object_fields, msg);
    }

    int iter(pb_field_iter_t *pS) override {
        return pb_field_iter_begin(pS, pb_ip175_fields, &data);
    }

    pb_ip175 data;
    bool linked[6];
    int32_t reg0[6];
    int32_t reg1[6];
    int phy;
    uint32_t last_tick;
};

extern IP175 ip175;
#endif //RTE_CSP280AV3_IP175_H
